#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void query_acc(void)
{
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDONLY|O_EXCL,0644);
	if(ERROR == fd)
	{
		perror("open acc");
		strcpy(stoc.str,"服务器正在升级，请耐心等待...");
	}
	else
	{
		Account aco;
		read(fd,&aco,sizeof(aco));
		sprintf(stoc.str,"当前余额为：%f",aco.money);
		printf("1.id=%d,pass=%d,name=%s,card=%s,money=%lf,errno=%d\n",aco.id,aco.pass,aco.name,aco.card,aco.money,aco.errno);
	}
	close(fd);
	stoc_send(&stoc);//返回消息
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_QUERY);
		stoc.type = ctos.pid;//反馈消息类型为进程号
		query_acc();
	}
}
